package com.bjqg.bjqg01.bjqg.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bjqg.bjqg01.bjqg.R;
import com.bjqg.bjqg01.bjqg.adapter.MessageAdapter;
import com.bjqg.bjqg01.bjqg.bean.Message;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by bjqg01 on 2017/12/13.
 */

public class OnlineMessageActivity extends BaseActivity{

    private EditText contents;
    private ImageView select;
    private TextView title;
    private ListView sendContent;
    private List<Message> listStr;
    private MessageAdapter adapter;
    private Message message;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_online_message);
        initView();
    }

    private void initView() {
        contents = (EditText) findViewById(R.id.contents);
        select= (ImageView) findViewById(R.id.select);
        title = (TextView) findViewById(R.id.tv_top_title);
        title.setText(getIntent().getStringExtra("OnlineMessageActivity"));
        select.setVisibility(View.INVISIBLE);


        sendContent = (ListView) findViewById(R.id.sendContent);
        listStr = new ArrayList<Message>();
        adapter = new MessageAdapter(this,listStr);
        sendContent.setAdapter(adapter);

        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.send).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
                String strings = contents.getText().toString();
                if (TextUtils.isEmpty(strings)){
                    Toast.makeText(OnlineMessageActivity.this,"留言不能为空！",Toast.LENGTH_LONG).show();
                }else if(!TextUtils.isEmpty(strings)){
                    Toast.makeText(OnlineMessageActivity.this,"提交成功！！！",Toast.LENGTH_LONG).show();
                    message = new Message(strings,R.mipmap.ic_launcher, sdf.format(new Date()));
                    listStr.add(message);
                    adapter.notifyDataSetChanged();
                    sendContent.setSelection(listStr.size());
                    contents.setText("");
//                    Intent intent=new Intent();
//                    intent.setClass(OnlineMessageActivity.this,InternetPalmActivity.class);
//                    startActivity(intent);
                }

            }
        });
    }
}
